package com.cloudkinto.bo.common;

import com.cloudkinto.common.common.DateUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * 分页通用参数
 *
 * @author long
 */
@Data
public class BaseQueryBo {
    /**
     * 当前页
     */
    public Integer current = 1;
    /**
     * 每页条数
     */
    public Integer pageSize = 20;

    /**
     * 公司id
     */
    public Long companyId;

    public Long tenantId;
    public Long userId;
    @ApiModelProperty("来源")
    public String source;
    public List<Long> ids;
    /**
     * 排序字段名
     */
    public String sortName;
    public String sortOrder = "DESC";
    @ApiModelProperty("仓库id")
    private Long whId;
    @ApiModelProperty("开始时间")
    private String startDate;

    @ApiModelProperty("结束时间")
    private String endDate;

    public Date getStartDate() {
        if (StringUtils.isBlank(startDate)) {
            return null;
        }
        if (DateUtils.regexCheckDate1(startDate)) {
            DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime dateTime = LocalDate.parse(startDate, format).atTime(0, 0, 0);
            return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
        }
        if (DateUtils.regexCheckDateTime1(startDate)) {
            DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime dateTime = LocalDateTime.parse(startDate, format);
            return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
        }
        return null;
    }

    public Date getEndDate() {
        if (StringUtils.isBlank(endDate)) {
            return null;
        }
        if (DateUtils.regexCheckDate1(endDate)) {
            DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime dateTime = LocalDate.parse(endDate, format).atTime(23, 59, 59);
            return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
        }
        if (DateUtils.regexCheckDateTime1(endDate)) {
            DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime dateTime = LocalDateTime.parse(endDate, format);
            return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
        }
        return null;
    }

}
